package com.bugstack.domain.activity.service.paytake;

import com.bugstack.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import com.bugstack.domain.activity.model.entity.ActivityEntity;
import com.bugstack.domain.activity.model.entity.RafflePayTakeEntity;
import com.bugstack.domain.activity.model.entity.UserRaffleOrderEntity;
import com.bugstack.domain.activity.reopsitory.IActivityRepository;
import com.bugstack.types.enums.ResponseCode;
import com.bugstack.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.concurrent.ExecutionException;

/**
 * @Author: czb
 * @Description: TODO
 * @DateTime: 2025/3/25 12:02
 **/
@Slf4j
public abstract class AbstractActivityPayTakeService implements IActivityPayTake {

    protected IActivityRepository activityRepository;

    protected AbstractActivityPayTakeService(IActivityRepository activityRepository) {
        this.activityRepository = activityRepository;
    }

    @Override
    public UserRaffleOrderEntity createOrder(Long activityId, String userId) throws ExecutionException, InterruptedException {
        RafflePayTakeEntity rafflePayTakeEntity = new RafflePayTakeEntity();
        rafflePayTakeEntity.setActivityId(activityId);
        rafflePayTakeEntity.setUserId(userId);
        return createOrder(rafflePayTakeEntity);
    }

    @Override
    public UserRaffleOrderEntity createOrder(RafflePayTakeEntity rafflePayTakeEntity) throws ExecutionException, InterruptedException {
        UserRaffleOrderEntity userRaffleOrderEntity;
        String userId = rafflePayTakeEntity.getUserId();
        Long activityId = rafflePayTakeEntity.getActivityId();
        Date currentDate = new Date();

        if (StringUtils.isBlank(userId) || null == activityId)
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());

        //1.校验活动开启时间
        ActivityEntity activity = activityRepository.queryRaffleActivityByActivityId(activityId);
        if (activity.getBeginDateTime().after(currentDate) || activity.getEndDateTime().before(currentDate))
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());

        //2.检查是否存在未使用订单
        userRaffleOrderEntity = activityRepository.queryNoUsedUserRaffleOrder(rafflePayTakeEntity);
        if (null != userRaffleOrderEntity){
            log.info("存在未消费订单 userId:{}, activityId:{}", userId, activityId);
            return userRaffleOrderEntity;
        }

        //3.查询账户总余额，月余额，日余额。
        CreatePartakeOrderAggregate aggregateObj = this.doFilterAccount(activityId, userId, currentDate);

        //3.创建订单
        userRaffleOrderEntity = this.createUserRaffleOrder(userId, activity, currentDate);
        aggregateObj.setUserRaffleOrderEntity(userRaffleOrderEntity);

        //保存聚合对象
        activityRepository.savePartakeOrderAggregate(aggregateObj);

        return userRaffleOrderEntity;
    }

    protected abstract UserRaffleOrderEntity createUserRaffleOrder(String userId, ActivityEntity activity, Date currentDate);


    protected abstract CreatePartakeOrderAggregate doFilterAccount(Long activityId, String userId, Date currentDate) throws ExecutionException, InterruptedException;
}
